package com.logisticsrepair;

import cn.hutool.core.date.DateUtil;

import com.logisticsrepair.util.mail.MailService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class JavaMailSenderTest {

    @Resource
    private JavaMailSender javaMailSender;

    @Autowired
    private MailService mailService;

    @Value("${spring.mail.username}")
    private String from;

    @Test
    public void test() {
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        simpleMailMessage.setFrom(from);
        simpleMailMessage.setTo("scitctocdtu@qq.com");
        simpleMailMessage.setSubject("邮件测试");
        simpleMailMessage.setText(DateUtil.date().toDateStr());
        simpleMailMessage.setSentDate(new Date());
        javaMailSender.send(simpleMailMessage);
    }


    @Test
    public void test1() {
        mailService.sendSimpleMail("scitctocdtu@qq.com", "邮件测试", "测试数据");
    }

    @Test
    public void test2() {
        String html = "<html><body><h3><font color=\"red\">" + "大家好，这是springboot发送的HTML邮件" + "</font></h3></body></html>";
        mailService.sendHtmlMail("scitctocdtu@qq.com", "邮件测试", html);
    }

    @Test
    public void test3() {
        String content = "<html><body><h3><font color=\"red\">" + "大家好，这是springboot发送的HTML邮件，有附件哦" + "</font></h3></body></html>";
        String[] filePath = new String[3];
        filePath[0] = "/Volumes/Document/Gitee/logistics-repair-system/SystemServer/logisticsrepair/src/main/resources/static/uploadImg/oGFtu6Y6YOKauWPMHZV36LXIQy9M/20220316223838/ia_100000044.jpg";
        mailService.sendAttachmentMail("scitctocdtu@qq.com", "邮件测试", content, "/Volumes/Document/Gitee/logistics-repair-system/SystemServer/logisticsrepair/src/main/resources/static/uploadImg/oGFtu6Y6YOKauWPMHZV36LXIQy9M/20220316223838/ia_100000044.jpg");
    }

    @Test
    public void test4() {
        String rscPath = "/Volumes/Document/Gitee/logistics-repair-system/SystemServer/logisticsrepair/src/main/resources/static/uploadImg/oGFtu6Y6YOKauWPMHZV36LXIQy9M/20220316223838/ia_100000046.jpg";
        String rscId = "001";
        String content = "<html><body><h3><font color=\"red\">" + "大家好，这是springboot发送的HTML邮件，有图片哦" + "</font></h3>"
                + "<img src=\'cid:" + rscId + "\'></body></html>";
        mailService.sendInlineResourceMail("scitctocdtu@qq.com", "邮件测试", content, rscPath, rscId);
    }

    @Test
    public void test5(){
        HashMap<String, Object> map = new HashMap<>();
        map.put("name", "张三");
        map.put("age", 18);
        map.put("sex", "男");
        mailService.sendHtmlTemplateMail("scitctocdtu@qq.com", "邮件测试", map, "EmailTemplate.html");
    }
}
